/**
 *
 * @author Jeison Hernandez
 * @date 04/03/2011
 *
 */
package circle;

import processing.core.*;

public class Midpoint extends PApplet {

    int xCenter, yCenter;

    public static void main(String[] args) {
        PApplet.main(new String[]{"circle.Midpoint"});
    }

    @Override
    public void setup() {
        size(800, 600);
        background(30, 75, 143);
        stroke(255);
    }

    @Override
    public void draw() {
    }

    @Override
    public void mousePressed() {
        xCenter = mouseX;
        yCenter = mouseY;
    }

    @Override
    public void mouseDragged() {
        int radius, h, b;
        background(30, 75, 143);
        b = abs(xCenter - mouseX);
        h = abs(yCenter - mouseY);
        radius = (int) Math.sqrt((b * b) + (h * h));
        midpointCircle2(xCenter, yCenter, radius);
    }

    public void simetry(int x, int y, int xStart, int yStart) {
        point(x + xStart, y + yStart);
        point(y + xStart, x + yStart);
        point(y + xStart, (-x) + yStart);
        point(x + xStart, (-y) + yStart);
        point((-x) + xStart, (-y) + yStart);
        point((-y) + xStart, (-x) + yStart);
        point((-y) + xStart, x + yStart);
        point((-x) + xStart, y + yStart);

    }

    public void midpointCircle(int xStart, int yStart, int radius) {
        int x, y;
        float d;
        x = 0;
        y = radius;
        d = (float) (5.0 / 4 - radius);
        simetry(x, y, xStart, yStart);
        while (y > x) {
            if (d < 0) {
                d += x * 2.0 + 3;
                x++;
            } else {
                d += (x - y) * 2.0 + 5;
                x++;
                y--;
            }
            simetry(x, y, xStart, yStart);
        }
    }

    public void midpointCircle1(int xStart, int yStart, int radius) {
        int x, y;
        int d;
        x = 0;
        y = radius;
        d = 1 - radius;
        simetry(x, y, xStart, yStart);
        while (y > x) {
            if (d <= 0) {
                d += x * 2 + 3;
                x++;
            } else {
                d += (x - y) * 2 + 5;
                x++;
                y--;
            }
            simetry(x, y, xStart, yStart);
        }
    }

    public void midpointCircle2(int xStart, int yStart, int radius) {
        int x, y;
        int d, dE, dSE;
        x = 0;
        y = radius;
        d = 1 - radius;
        dE = 3;
        dSE = 5 - radius * 2;
        simetry(x, y, xStart, yStart);
        while (y > x) {
            if (d <= 0) {
                d += dE;
                dE += 2;
                dSE +=2;
                x++;
            } else {
                d += dSE;
                dE +=2;
                dSE +=4;
                x++;
                y--;
            }
            simetry(x, y, xStart, yStart);
        }
    }
}
